﻿using CatProxy.Common;
using EpubFactory.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace EpubFactory.Hetushu
{
    public class HetushuRepository : IRepository<HetushuBook>
    {
        private static HttpClient _http = new HttpClient();
        static HetushuRepository()
        {
            _http.DefaultRequestHeaders.Add("UserAgent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0");
            _http.DefaultRequestHeaders.Add("Referer", "http://www.99lib.net");
        }
        public HetushuBook FindOne(Expression<Func<HetushuBook>> predicate)
        {
            throw new NotImplementedException();
        }

        public HetushuBook FindOne(string id)
        {
            throw new NotImplementedException();
        }

        public Pagination<HetushuBook> Search(string q, int page = 1)
        {
            string url = string.Format("http://hetushu.com/book/search.php?keyword={0}&page={1}", q, page);
            var model = new Pagination<HetushuBook>()
            {
                Items = new List<HetushuBook>()
            };
            var html = _http.GetStringAsync(url).Result;
            var doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(html);

            var dlTag = doc.DocumentNode.Descendants("dl").FirstOrDefault();
            if (dlTag != null)
            {
                var dtTagText = dlTag.Descendants("dt").Select(g => g.InnerText).FirstOrDefault();

                if (dtTagText != null)
                {
                    var s = dtTagText.IndexOf("共搜索到");
                    var e = dtTagText.IndexOf("本");
                    model.Total = Int32.Parse(dtTagText.Substring(s + 4, e - s - 4));
                }

                foreach (var ddTag in dlTag.Descendants("dd"))
                {
                    var book = new HetushuBook();
                    var h4 = ddTag.Descendants("h4").FirstOrDefault();

                    var link = h4.Descendants("a").FirstOrDefault();
                    if (link != null)
                    {
                        book.Title = link.InnerText;
                        book.SourceUrl = link.Attributes["href"].Value;
                    }
                    var author = h4.Descendants("span").Select(g => g.InnerText).FirstOrDefault();
                    if (!string.IsNullOrWhiteSpace(author))
                    {
                        book.Authors.Add(author.Replace("/", "").Trim());
                    }
                    book.CoverUrl = ddTag.Descendants("img").Select(g => g.Attributes["src"].Value).FirstOrDefault();
                    book.Summary = ddTag.Descendants("div").Where(g => g.HasClass("intro")).Select(g => g.InnerHtml).FirstOrDefault();


                    model.Items.Add(book);
                }
            }
            model.PageSize = 16;
            model.PageNo = page;
            int i = 1;
            foreach (var item in model.Items)
            {
                item.Seq = i++;
            }
            return model;
        }
    }
    public class HetushuBook : Book
    {
        public int Seq { get; set; }
        public string SourceUrl { get; set; }
        public string Author { get { return string.Join(",", this.Authors); } }
    }
}
